package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.ClubSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.MatchSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.ProSimpbean;
import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.Other.CacheInfo;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.Service.ClubService;
import com.neusoft.yuejian26.yuejian_26.Service.MatchService;
import com.neusoft.yuejian26.yuejian_26.Service.ProService;
import com.neusoft.yuejian26.yuejian_26.model.AttentionModel;
import com.neusoft.yuejian26.yuejian_26.model.ClubModel;
import com.neusoft.yuejian26.yuejian_26.model.ProModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitHelper;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProSimpActivity extends AppCompatActivity {

    TextView pro_name,simp_pro_info,simp_pro_club;
    ImageView pro_simp_pic,svg_back,home_pro_attention;
    String mid=null;
    String uid=null;
    String xid;
    String flag=String.valueOf(1);
    int attentionState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_simp);
        intview();
        intEvent();
        getlastActivityData();
        getProSimpData();
    }

    private void getProSimpData() {
        uid=new CacheInfo(ProSimpActivity.this).getUserIdCache();
        ProModel proModel = new ProModel();
        Call call = proModel.proSimp(mid);
        proModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                ProSimpbean mpsimpdata= (ProSimpbean) o;
                getClubName(mpsimpdata.getClubid());
                Picasso.with(ProSimpActivity.this)
                        .load("http://115.159.93.244/new18yuejian/Public/upload/"+mpsimpdata.getMpic())
                        .into(pro_simp_pic);
                pro_name.setText(mpsimpdata.getMname());
                simp_pro_info.setText(mpsimpdata.getMintro());
                getAttentionState();
            }
        });
    }
    private void getClubName(String clubid){
        ClubModel clubModel = new ClubModel();
        Call call = clubModel.clubSimp(clubid);
        clubModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                ClubSimpbean clsimpdata= (ClubSimpbean) o;
                simp_pro_club.setText(clsimpdata.getClubname());
            }
        });
    }

    private void getlastActivityData(){
        Bundle bundle=this.getIntent().getExtras();
        mid = bundle.getString("proiid");
        pro_name.setText( bundle.getString("proname"));
    }

    private void getAttentionState(){
        uid = new CacheInfo(ProSimpActivity.this).getUserIdCache();
        Bundle bundle=this.getIntent().getExtras();
        xid = bundle.getString("proiid");
        AttentionModel attentionModel = new AttentionModel();
        Call call = attentionModel.checkState(uid,xid,flag);
        attentionModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                Result checkState = (Result) o;
                int state = checkState.getCode();
                attentionState = state;
                if(1==state){
                      home_pro_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                }
                else {
                      home_pro_attention.setImageResource(R.drawable.ic_home_attention);
                }
            }
        });
    }

    private class doAttention implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            AttentionModel attentionModel = new AttentionModel();
            Call call = attentionModel.doAttention(uid,xid,flag);
            attentionModel.bindCallback(call, new RetrofitCallback() {
                @Override
                public void onResponse(Object o) {
                    Result doAttention = (Result) o;
                    int states = doAttention.getCode();
                    if (1==states){
                       if (attentionState==1){
                           Toast.makeText(ProSimpActivity.this,"关注大师成功",Toast.LENGTH_LONG).show();
                           attentionState = 0;
                           home_pro_attention.setImageResource(R.drawable.ic_home_attention);
                       }
                       else {
                           Toast.makeText(ProSimpActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                           attentionState = 1;
                           home_pro_attention.setImageResource(R.drawable.ic_home_attention_unchecked);
                       }
                    }
                }
            });
        }
    }

    private void intview(){
        pro_name= (TextView) findViewById(R.id.pro_name);
        simp_pro_info= (TextView) findViewById(R.id.simp_pro_info);
        simp_pro_club= (TextView) findViewById(R.id.simp_pro_club);
        pro_simp_pic = (ImageView) findViewById(R.id.pro_simp_pic);
        svg_back= (ImageView) findViewById(R.id.svg_back);
        home_pro_attention= (ImageView) findViewById(R.id.home_pro_attention);
    }

    private void intEvent(){
        home_pro_attention.setOnClickListener(new doAttention());
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ProSimpActivity.this,MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("page",0);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
    }

}
